package
{
	import flash.display.Bitmap;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.utils.Dictionary;
	
	import levelloader.TiledMap;
	
	import starling.events.Event;
	import starling.textures.Texture;
	import starling.textures.TextureAtlas;

	public class Assets
	{
		// Texture Atlas
		[Embed(source="../media/textures/animatlas2.png")] 
		public static const animTexture:Class;		
		[Embed(source="../media/textures/ui.png")] 
		public static const uiTexture:Class;		
		[Embed(source="../media/textures/loading.png")] 
		public static const LoadingTexture:Class;		
				
		[Embed(source = "../media/textures/playscreen.png")]
		public static const PlayScreen:Class;
		[Embed(source = "../media/textures/seasonselect.png")]
		public static const SeasonSelect:Class;
		[Embed(source = "../media/textures/hlevel.png")]
		public static const LevelSelect:Class;
		
		// Sound
		[Embed (source="../media/Sound/01.mp3")]
		public static const one:Class;
		[Embed (source="../media/Sound/22.mp3")]
		public static const two:Class;
		[Embed (source="../media/Sound/Jump.mp3")]
		public static const jump:Class;
		[Embed (source="../media/Sound/attack.mp3")]
		public static const attack:Class;
		[Embed (source="../media/Sound/33.mp3")]
		public static const three:Class;
		[Embed (source="../media/Sound/coin.mp3")]
		public static const coin:Class;
		
		//font
		[Embed(source = "../media/font/angrybirds_0.png")]
		public static const font:Class;
		[Embed(source="../media/font/angrybirds.fnt",mimeType="application/octet-stream")]
		public static const fontxml:Class;
	
		
		private static var sSounds:Dictionary=new Dictionary();											
		private static var sTextureHalloweenAtlas:TextureAtlas;
		private static var sTextureskeleton:TextureAtlas;
		private static var sTextureUI:TextureAtlas;
		public static var SoundStore:Sound= new one() as Sound;
		public  static var SoundStore2:Sound= new two() as Sound;
		public  static var SoundStore3:Sound= new jump() as Sound;
		public  static var SoundStore4:Sound= new attack() as Sound;
		public  static var SoundStore5:Sound= new three() as Sound;
		public  static var SoundStore6:Sound= new coin() as Sound;

		//XML
		[Embed(source="../media/textures/playscreen.pex", mimeType="application/octet-stream")]
		public static const SeasonSelectpx:Class;
		[Embed(source="../media/textures/seasonselect.pex", mimeType="application/octet-stream")]
		public static const PlayScreenpx:Class;
		[Embed(source="../media/textures/hlevel.pex", mimeType="application/octet-stream")]
		public static const LevelSelectpx:Class;
		
		[Embed(source="../media/textures/animatlas2.xml", mimeType="application/octet-stream")] 
		public static const skeletonXml:Class; 
		[Embed(source="../media/textures/ui.xml", mimeType="application/octet-stream")] 
		public static const uiXml:Class;
		
		/*********************
		 * LEVEL RESOURCES
		*********************/
		
		//level alas texture
		[Embed(source="../media/textures/halloween.png")]				
		public static const halloweenTexture:Class;
		[Embed(source="../media/textures/flower_mania.png")]
		public static const flowermaniaTexture:Class;
		[Embed(source="../media/textures/fishtown_levels.png")]
		public static const fishtownTexture:Class;
		//level atlas XML
		[Embed(source="../media/textures/halloween.xml", mimeType="application/octet-stream")]		
		public static const halloweenXml:Class;
		[Embed(source="../media/textures/flower_mania.xml", mimeType="application/octet-stream")]
		public static const flowermaniaXml:Class;
		[Embed(source="../media/textures/fishtown_levels.xml", mimeType="application/octet-stream")]
		public static const fishtownXml:Class;		
		//level map XML							
		[Embed(source="../media/textures/halloween_1_1.tmx", mimeType="application/octet-stream")]				
		public static var halloween1MapXML:Class;
		[Embed(source="../media/textures/halloween_2.tmx", mimeType="application/octet-stream")]				
		public static var halloween2MapXML:Class;
		[Embed(source="../media/textures/halloween_3.tmx", mimeType="application/octet-stream")]				
		public static var halloween3MapXML:Class;		
		[Embed(source="../media/textures/flowermania1_1.tmx", mimeType="application/octet-stream")]
		public static var flowermania1MapXML:Class;
		[Embed(source="../media/textures/flowermania_2.tmx", mimeType="application/octet-stream")]
		public static var flowermania2MapXML:Class;
		[Embed(source="../media/textures/flowermania_3.tmx", mimeType="application/octet-stream")]
		public static var flowermania3MapXML:Class;
		[Embed(source="../media/textures/fishtown.tmx", mimeType="application/octet-stream")]
		public static var fishtown1MapXML:Class;
		[Embed(source="../media/textures/fishtown_level_2.tmx", mimeType="application/octet-stream")]
		public static var fishtown2MapXML:Class;
		[Embed(source="../media/textures/fishtown_level_3.tmx", mimeType="application/octet-stream")]
		public static var fishtown3MapXML:Class;
				
		
		// Texture cache
		private static var sTextures:Dictionary=new Dictionary();

		/**
		 * Returns the Texture atlas instance.
		 * @return the TextureAtlas instance (there is only oneinstance per app)
		 */
		
		public static const BACKGROUND:uint = 0;
		public static const MIDLAYER:uint = 1;
		public static const PLATFORM:uint = 2;
		public static const ENEMIES:uint = 3;
		public static const ANIMATIONS:uint = 4;
		public static const COLLECTABLES:uint = 5;

		public static const SEASONS_ATLAS_TEXTURE:Array = new Array ("", "halloweenTexture", "flowermaniaTexture", "fishtownTexture");
		public static const SEASONS_ATLAS_XML:Array = new Array ("", "halloweenXml", "flowermaniaXml", "fishtownXml");
		public static const SEASONS_MAPS:Array = new Array ("", "halloween1MapXML", "halloween2MapXML", "halloween3MapXML",
																"flowermania1MapXML", "flowermania2MapXML", "flowermania3MapXML", 
																"fishtown1MapXML", "fishtown2MapXML", "fishtown3MapXML"	);
		public static const LAYER_NAME:Array = new Array ("background", "midlayer", "platform", "enemies");
		//public static const SOUNDS_LEVELS:Array= new Array ("","level1","level2","level3","level4","level5","level6",
															//"level7","level8","level9");
		public static function getLevelMapAtlas():TextureAtlas
		{
			if (sTextureHalloweenAtlas == null)
			{
				var texture:Texture=getTexture(SEASONS_ATLAS_TEXTURE[Game.season]);
				var xml:XML=XML(new Assets[SEASONS_ATLAS_XML[Game.season]]());
				sTextureHalloweenAtlas=new TextureAtlas(texture, xml);
				
			}
			
			return sTextureHalloweenAtlas;
		}

		public static function getskeleton():TextureAtlas
		{
			if (sTextureskeleton == null)
			{
				var texture:Texture=getTexture("animTexture");
				var xml:XML=XML(new skeletonXml());
				sTextureskeleton=new TextureAtlas(texture, xml);
			}
			return sTextureskeleton;
		}

		public static function getUIScreens():TextureAtlas
		{
			if (sTextureUI == null)
			{
				var texture:Texture=getTexture("uiTexture");
				var xml:XML=XML(new uiXml());
				sTextureUI=new TextureAtlas(texture, xml);
			}
			return sTextureUI;
		}

		/**
		 * 
		 * @param name
		 * @return 
		 * @throws ArgumentError
		 */
		public static function getSound(name:String):Sound
		{
			var sound:Sound=sSounds[name] as Sound;
			if (sound)
				return sound;
			else
				throw new ArgumentError("Sound not found: " + name);
		}

		/**
		 * Returns a texture from a texture atlas based on a string key.
		 * 
		 * @param name A key that matches a static constant of Bitmap type.
		 * @return a starling texture.
		 */
		public static function getTexture(name:String):Texture
		{
			if (sTextures[name] == undefined)
			{
				var bitmap:Bitmap=new Assets[name]();
				sTextures[name]=Texture.fromBitmap(bitmap);
			}

			return sTextures[name];
		}

		/**
		 * 
		 */
		public static function prepareSounds():void
		{
			return;
			//sSounds["Step"] = new StepSound();   
		}
		
		public static function dispose():void
		{
			getskeleton().dispose();			
					
			sTextureHalloweenAtlas = null;
			sTextureskeleton = null;
			
			sTextures = new Dictionary();
		}		
	}
}
